<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
            background-color: #f4f4f9;
        }

        .register-container {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 300px;
        }

        .register-container h2 {
            text-align: center;
            margin-bottom: 20px;
        }

        .register-container label {
            display: block;
            margin-bottom: 5px;
        }

        .register-container input {
            width: 100%;
            padding: 8px;
            margin-bottom: 15px;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
        }

        .register-container input[type="submit"] {
            background-color: #4CAF50;
            color: white;
            cursor: pointer;
        }

        .register-container input[type="submit"]:hover {
            background-color: #45a049;
        }
    </style>
</head>

<body>
<div class="register-container">
    <h2>用户注册</h2>
    <form id="registerForm" action="<%=request.getContextPath()%>/register" method="post">
        <label for="username">用户名:</label>
        <input type="text" id="username" name="username" required placeholder="请输入用户名"><br>
        <label for="password">密码:</label>
        <input type="password" id="password" name="password" required placeholder="请输入密码"><br>
        <label for="role">角色:</label>
        <select id="role" name="role">
            <option value="Admin">管理员</option>
            <option value="Staff" selected>员工</option>
        </select><br>
        <div id="adminPasswordContainer" style="display: none;">
            <label for="adminPassword">管理员密码:</label>
            <input type="password" id="adminPassword" name="adminPassword" placeholder="请输入管理员密码"><br>
        </div>
        <input type="submit" value="注册">
    </form>
</div>
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<script>
    $(document).ready(function () {
        // 监听角色选择的变化
        $("#role").change(function () {
            if ($(this).val() === "Admin") {
                $("#adminPasswordContainer").show();
            } else {
                $("#adminPasswordContainer").hide();
            }
        });

        // 初始化时根据默认选择的角色显示或隐藏管理员密码输入框
        $("#role").trigger("change");

        // 提交表单时的处理
        $("#registerForm").submit(function (event) {
            event.preventDefault();
            var formData = $(this).serialize();
            console.log(formData);
            $.post("<%=request.getContextPath()%>/register", formData, function (data) {
                console.log("注册结果：" + data);
                if (data === "success") {
                    alert("注册成功，请登录");
                    window.location.href = "login.jsp";
                } else if (data === "管理员密码错误") {
                    alert("管理员密码错误，请重试");
                } else {
                    alert("注册失败，请检查输入信息并重试");
                }
            });
        });
    });
</script>
</body>

</html>
